package com.zwy.fornote.controller;

import com.github.pagehelper.PageInfo;
import com.zwy.fornote.entity.req.*;
import com.zwy.fornote.entity.vo.CountUserArticleVO;
import com.zwy.fornote.entity.vo.QuestionAndAnswerByUser;
import com.zwy.fornote.entity.vo.UserManageVO;
import com.zwy.fornote.model.User;
import com.zwy.fornote.service.UserService;
import com.zwy.fornote.util.AjaxResult;
import com.zwy.fornote.util.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *User Controller
 *
 * @author zhuyi 1205242
 * @version UserController 2021/3/4 17:25
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
//@SessionAttributes("user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/getUserById/{id}")
    public User getUser(@PathVariable int id)
    {
        return userService.getUserByID(id);
    }

    @RequestMapping("/lockUser/{id}")
    public AjaxResult lockUser(@PathVariable int id)
    {
        if(userService.checkUserStatus(id)==0)
        {
            return new AjaxResult(-1,"用户已被封禁 请勿重复操作!","");
        }
        return AjaxResult.ok(userService.lockUser(id));
    }

    @RequestMapping("/unlock/{id}")
    public AjaxResult unlock(@PathVariable int id)
    {
        if(userService.checkUserStatus(id)==1)
        {
            return new AjaxResult(-1,"用户未被封禁 请勿重复操作!","");
        }
        return AjaxResult.ok(userService.unlockUser(id));
    }

    @PostMapping("/login")
    public AjaxResult logIn(@RequestBody LogInReq req)
    {
        try {
            User user = userService.getUserByAccountAndPassword(req.getAccount(), req.getPassword());
            if(user!=null)
            {
                if(userService.checkUserStatus(user.getId())==0)
                {
                    return AjaxResult.fail(-2,"您已被封禁！");
                }
                CurrentUser.setUser(user);
                System.out.println("保存用户");
                return AjaxResult.ok(user);
            }else
            {
                return AjaxResult.fail(-1,"用户不存在！");
            }
        }catch (Exception e)
        {
            return AjaxResult.fail(-1,e.getMessage());
        }
    }

    @PostMapping("/register")
    public AjaxResult createNewUser(@RequestBody NewUserReq req)
    {
        try {
            //todo 判断账号是否存在，存在不让注册
            int newUser = userService.createNewUser(req);
            if(newUser>0)
            {
                //保存注册用户为当前用户
                CurrentUser.setUser(userService.getUserByAccountAndPassword(req.getAccount(), req.getPassword()));
                return AjaxResult.ok("注册成功，已为您自动登录！");
            }else
            {
                return AjaxResult.fail(-1,"注册失败！");
            }
        }catch (Exception e)
        {
            return AjaxResult.fail(-1,e.getMessage());
        }
    }
    @GetMapping("/getCurrent")
    public AjaxResult getCurrent(Model model)
    {

        User user=CurrentUser.getUser();
//        System.out.println(user);
        if(user!=null)
        {
            return AjaxResult.ok(user);
        }else
        {
            return AjaxResult.fail(-1,"用户不存在");
        }

    }
    @GetMapping("/logOut")
    public AjaxResult logOut()
    {
        CurrentUser.remove();
        return AjaxResult.ok("您已退出登录！");
    }
    @GetMapping("/getAnswer")
    public AjaxResult getUserAnswerAndQuestion()
    {
        try {
            List<QuestionAndAnswerByUser> userAnswerAndQuestion = userService.getUserAnswerAndQuestion(CurrentUser.getUser().getId());
            return AjaxResult.ok(userAnswerAndQuestion);
        }catch (Exception e)
        {
            return AjaxResult.fail(-1,e.getMessage());
        }
    }

    @PostMapping("/changeHeadPic")
    public AjaxResult changeHeadPic(@RequestBody ChangePicReq req)
    {
        try {
            int i = userService.changeHeadPic(req.getUserId(), req.getHeadPic());
            if(i>0)
            {
                return AjaxResult.ok("更换头像成功");
            }else
            {
                return AjaxResult.fail(-1,"更换头像失败！");
            }
        }catch (Exception e)
        {
            return AjaxResult.fail(-1,e.getMessage());
        }
    }

    @PostMapping("/manageUser")
    public AjaxResult manageUser(@RequestBody UserManageReq req)
    {
        try {
            PageInfo<UserManageVO> pageInfo = userService.selectManageUserList(req);
            return AjaxResult.ok(pageInfo);

        }catch (Exception e)
        {
            return AjaxResult.fail(-1,e.getMessage());
        }
    }

    @PostMapping("/countUser")
    public AjaxResult getCountUserArticle(@RequestBody PageReq req)
    {
        try {
            PageInfo<CountUserArticleVO> pageInfo = userService.getCountUserArticle(req);
            return AjaxResult.ok(pageInfo);

        }catch (Exception e)
        {
            return AjaxResult.fail(-1,e.getMessage());
        }
    }

    @GetMapping("/analysis")
    public AjaxResult getAnalysis()
    {
        try {
            return AjaxResult.ok(userService.getAnalysis());

        }catch (Exception e)
        {
            return AjaxResult.fail(-1,e.getMessage());
        }
    }


}
